package routes

import (
	"itodo/core/common/errorcode"
	"itodo/core/localwrap"

	"github.com/RocksonZeta/irisx"
	"github.com/RocksonZeta/wrap/errs"
)

func Check(ctx *irisx.Context) {
	fieldErrors := ctx.ParamErrors()
	if len(fieldErrors) > 0 {
		e := errs.NewUserError(errorcode.ParamError, "request parameter error")
		e.Data = fieldErrors
		panic(e)
	}
}

func SessionSet(ctx *irisx.Context, key string, value interface{}, secs int) {
	red := localwrap.GetRedis()
	defer red.Close()
	rkey := ctx.Sid() + "/" + key
	red.SetJson(rkey, value, secs)
}
func SessionGet(ctx *irisx.Context, key string, result interface{}) {
	red := localwrap.GetRedis()
	defer red.Close()
	rkey := ctx.Sid() + "/" + key
	red.GetJson(rkey, result)
}
func SessionGetString(ctx *irisx.Context, key string) string {
	var result string
	SessionGet(ctx, key, &result)
	return result
}
func SessionGetInt(ctx *irisx.Context, key string) int {
	var result int
	SessionGet(ctx, key, &result)
	return result
}
func ReturnError(ctx *irisx.Context, e *errs.Err) {
	ctx.JSON(e)
}
